package com.bwf.ticket;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/5/19 15:52
 * @Copyright 博为峰
 */
public class Ticket04 implements Runnable {
    private static int num = 100;
    final Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            m2();
        }
    }

    //同步方法：当前对象（this）
    private synchronized void m1() {
        if (num > 0) {//2
            try {
                //t0 1   t1 0
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ":num=" + num--);
        } else {
        }
    }

    //静态方法：字节码文件对象
    private static synchronized void m2() {
        if (num > 0) {//2
            try {
                //t0 1   t1 0
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ":num=" + num--);
        } else {
        }
    }
}
